import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/views/Layout/index.vue'
import Login from '@/views/Login/index.vue'
import NotFound from '@/views/404/index.vue'

export let constantRoutes =[
  {
    path:'/login',
    component:Login,
    hidden:true
  },
  {
    path:'/404',
    component:NotFound,
    hidden:true
  },
  { path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound },

  // {
  //   path:'/',
  //   component:Layout,
  //   redirect:'/home',
  //   children:[
  //     {
  //       path:'/home',
  //       name:'home',
  //       component:Home,
  //       meta:{title:'首页',icon:'house',type:'primary'}
  //     },
  //     {
  //       path:'/user',
  //       component:()=>import('@/views/User/Users.vue'),
  //       meta:{title:'用户',icon:'UserFilled',type: "success" }
  //     },
  //     {
  //       path:'/role',
  //       component:()=>import('@/views/Role/index.vue'),
  //       meta:{title:'角色',icon:'operation', type: "warning"}
  //     },
  //     {
  //       path:'/department',
  //       component:()=>import('@/views/Department/Department.vue'),
  //       meta:{title:'部门',icon:'HomeFilled', type: "info" }
  //     },
  //     {
  //       path:'/menu',
  //       component:()=>import('@/views/Menu/index.vue'),
  //       meta:{title:'菜单',icon:'menu', type: "danger"}
  //     },
  //     {
  //       path:'/test',
  //       // component:Layout,//()=>import('@/views/TestView/index.vue'),
  //       meta:{title:'测试',icon:'link'},
  //       children:[
  //         {
  //           path:'/test/test1',
  //           name:'test1',
  //           component:()=>import('@/views/TestView/index.vue'),
  //           meta:{title:'测试',icon:'link'},
  //         }
  //       ]
  //     },
  //   ]
  // }
]

let router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes:constantRoutes
})

// 后台返回路由匹配view中的页面
export const filterAsyncRouter=(asyncRouteMap)=>{
  // console.log(asyncRouteMap)
  const accessedRouters =  asyncRouteMap.filter(route=>{
    if (route.path && !route.IsButton) {
      if (route.path === '/' || route.path === '-') {//Layout组件特殊处理
          route.component = Layout
      } else {
        try {
          route.component = () => import('@/views' + route.path.replace('/:id','') + '.vue');
      } catch (error) {
          console.info('%c 当前路由 ' + route.path.replace('/:id','') + '.vue 不存在，因此如法导入组件，请检查接口数据和组件是否匹配，并重新登录，清空缓存!', "color:red")
      }
      }
    }
    if (route.children && route.children.length && !route.IsButton) {
        route.children = filterAsyncRouter(route.children)
    }
    return true
  })
  return accessedRouters
}

/**
 * 过滤返回的路由数据，去掉是按钮的数据
 * @param {*} params 后台拿到的路由数组
 * @returns 返回路由菜单
 */
export const filterRoute = (params)=>{
  var f = item => {
    if (item['children']) {
        item['children'] = item['children'].filter(f);
        return true;
    } else if (item['IsButton']) {
        return item['IsButton']===false;
    }  else {
        return true;
    }
  }
  return params.filter(f);
}


export default router
